package member.base.action;

import m.common.action.ActionRequest;
import m.common.action.Param;
import m.common.action.RequestMethod;
import m.common.model.util.*;
import m.system.util.JSONMessage;
import member.base.model.ServeGoods;
import member.base.model.StatusModel;
import member.base.model.UserInfo;
import member.base.util.PowerUtil;

@ActionRequest(name = "action/base/serveGoods",description = "服务商品")
public class ServeGoodsAction extends BaseAction{

    @RequestMethod(description = "列表")
    public JSONMessage getList() throws Exception {
        ModelQueryList queryList=toQueryList(ServeGoods.class,"*","type.*","store.name","store.color");
        queryList.addCondition(QueryCondition.eq("business.oid", PowerUtil.getBusiness().getOid()));
        return JSONMessage.success("查询成功").push("page",ModelQueryList.getModelPageInfo(queryList));
    }
    @RequestMethod(description = "店铺列表")
    public JSONMessage getStoreList() throws Exception {
        ModelQueryList queryList=toQueryList(ServeGoods.class,"*","type.*","store.name","store.color");
        queryList.addCondition(QueryCondition.eq("business.oid", PowerUtil.getBusiness().getOid()));
        queryList.addCondition(QueryCondition.or(
            QueryCondition.isEmpty("store.oid"),
            QueryCondition.eq("store.oid",PowerUtil.getStore().getOid())
        ));
        return JSONMessage.success("查询成功").push("page",ModelQueryList.getModelPageInfo(queryList));
    }
    @RequestMethod(description = "选择列表")
    public JSONMessage getSelectList() throws Exception {
        ModelQueryList queryList=toQueryList(ServeGoods.class,"*","type.*","store.name","store.color");
        queryList.addCondition(QueryCondition.eq("business.oid", PowerUtil.getBusiness().getOid()));
        queryList.addCondition(QueryCondition.eq("status", StatusModel.StatusEnabled));
        queryList.addCondition(QueryCondition.or(
           QueryCondition.isEmpty("store.oid"),
           QueryCondition.eq("store.oid",PowerUtil.getStore().getOid())
        ));
        return JSONMessage.success("查询成功").push("page",ModelQueryList.getModelPageInfo(queryList));
    }
    @RequestMethod(description = "保存")
    public JSONMessage save(@Param("model") ServeGoods model) throws Exception {
        PowerUtil.verifyPower(UserInfo.PowerManage,UserInfo.PowerStore);
        model.setBusiness(PowerUtil.verifyBusiness(model.getBusiness()));
        ModelQueryCheck.init(model)
            .checkNotNull("type.oid","服务类型不能为空")
            .checkNotNull("title","标题不能为空")
            .checkNotNull("price","单价不能为空")
            .checkNumberRange("discount",0.0,100.0,"优惠比例范围错误（0~100）")
            .doCheck(false,true);
        ModelCheckUtil.checkUniqueCombine(model,new String[]{"business.oid","title"},"标题已存在");
        ModelUpdateUtil.saveModel(model);
        return JSONMessage.success("保存成功");
    }
}
